﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace TRAC_Demo
{
    // T.R.A.C.
    // Main Author(s): Morgan Davis
    // Additional Authors:
    // Date: 9/30/2013
    // Update: 10/25/2013 users can now select issue from treeview dialog and select agent/employee from dialog box
    // Recent Update 11/2/2013 Added autofill function to creating agent box

    public partial class FormGenerateTicket : Form
    {
        #region [ Fields ]
        // following variables are used to store the integers that are extracted from the textboxes
        string employeeString = "";
        string assignedString = "";
        string creatingString = "";

        #endregion

        #region [ Constructors ]

        public FormGenerateTicket()
        {
            InitializeComponent();
            this.BackColor = CurrentUser.UserColor;
        }

        #endregion

        #region [ Events ]

        private void FormGenerate_Load(object sender, EventArgs e)
        {
            comboStatus.Text = "PENDING";
            comboPriority.SelectedIndex = 2;
            txtCreatingAgent.Text = CurrentUser.EmpID.ToString().PadRight(5) + ": " + CurrentUser.EmpLastName + ", " + CurrentUser.EmpFirstName;
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            //check to ensure all required fiels are filled out before proceeding with submit action
            if (txtEmpId.Text != "Click to Select" && txtCreatingAgent.Text != "" && txtAssignedAgent.Text != "Click to Select")
            {
                //dialog box used to verify if clicking the generation button was intentional or desired
                DialogResult myResult = MessageBox.Show("Are you sure you want to generate this ticket?", "Verify Ticket Generation", MessageBoxButtons.YesNo);
                if (myResult == DialogResult.Yes)
                {
                    try
                    {
                        CreateTicket();
                        this.Close();
                    }
                    catch (SqlException)
                    {
                        MessageBox.Show("Please ensure that all fields are filled out properly.", "Empty Field Entries");
                    }
                    catch (FormatException)
                    {
                        MessageBox.Show("Number values in text fields.", "Invalid Data Entry");
                    }

                }
                else
                {
                    MessageBox.Show("Ticket Generation Cancelled");
                }
            }
            else
            {
                //notify user that required fields are not all filled out
                MessageBox.Show("Please ensure all fields are filled out before submitting the ticket", "Please fill out all fields.");
            }
        }

        private void btnClose_Click(object sender, EventArgs e) //closes form
        {
            this.Close();
        }

        private void txtIssue_MouseClick(object sender, MouseEventArgs e) //brings up tree view form to select ticket issue
        {
            using (var Form = new FormIssueTree())
            {
                var result = Form.ShowDialog();
                if (result == DialogResult.OK)
                {
                    txtIssue.Text = Form.returnIssue;
                }
            }
        }

        private void txtEmpId_MouseClick(object sender, MouseEventArgs e) //calls form to select desired employee
        {
            using (var Form = new FormEmployeeList())
            {
                var result = Form.ShowDialog();
                if (result == DialogResult.OK)
                {
                    txtEmpId.Text = Form.returnEmployee;
                }
            }
        }

        private void txtAgentId_MouseClick(object sender, MouseEventArgs e) //calls forms to select assigned agent
        {
            using (var Form = new FormAgentList())
            {
                var result = Form.ShowDialog();
                if (result == DialogResult.OK)
                {
                    txtAssignedAgent.Text = Form.returnAgent;
                }
            }
        }

        #endregion

        #region [ Methods ]

        private void CreateTicket()
        {
            TRACobjectsDataContext ni = new TRACobjectsDataContext();
            for (int i = 0; i < 5; i++)
            {
                //following code blocks extracts integers from textboxes and places them in string variable to be parsed
                if (char.IsDigit(txtEmpId.Text[i]))
                {
                    employeeString += txtEmpId.Text[i];
                }
                if (char.IsDigit(txtAssignedAgent.Text[i]))
                {
                    assignedString += txtAssignedAgent.Text[i];
                }
                if (char.IsDigit(txtCreatingAgent.Text[i]))
                {
                    creatingString += txtCreatingAgent.Text[i];
                }
            }

            Ticket newTicket = new Ticket() //creates new ticket object to be added to the database
            {              
                EmployeeID = int.Parse(employeeString),
                DateCreated = DateTime.Now,
                CreatingAgent = int.Parse(creatingString),
                SitCode = txtIssue.Text,
                PriorityCode = comboPriority.Text,
                AssignedAgent = int.Parse(assignedString),
                StatusCode = comboStatus.Text,
                NoteField = txtNotes.Text
            };

            ni.Tickets.InsertOnSubmit(newTicket);
            ni.SubmitChanges();
            ni.Dispose(); //remove linq object from memory
        }

        #endregion
    }
}
